package com.hongxun.budget.ui.activity.sg;

import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.hongxun.budget.R;
import com.hongxun.budget.ui.activity.BaseActivity;
import com.hongxun.budget.ui.dialog.ListDialog;
import com.hongxun.budget.ui.fragment.sg.BrowseAnchorPitInfo;
import com.hongxun.budget.ui.fragment.sg.BrowseBasicsWorkInfo;
import com.hongxun.budget.ui.fragment.sg.BrowseGroundInfo;
import com.hongxun.budget.ui.fragment.sg.BrowsePolePitInfo;
import com.hongxun.budget.utils.RecyclerViewUtils;
import com.hongxun.budget.utils.SPUtils;
import com.hongxun.budget.utils.SgDBUtils;
import com.hongxun.budget.utils.SgStrKey;
import com.hongxun.budget.utils.XUtils;
import com.hongxun.entity.ImageUrl;
import com.hongxun.greenentity.tk.DPictureEntity;
import com.hongxun.interfaces.ResponseListener;
import com.jessewu.library.SuperAdapter;
import com.jessewu.library.view.ViewHolder;
import com.previewlibrary.GPreviewBuilder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 隐蔽工程信息
 */
public class HideProInfoBrowseActivity extends BaseActivity {
    @BindView(R.id.tv1)
    TextView tv1;
    @BindView(R.id.tv2)
    TextView tv2;
    @BindView(R.id.tv3)
    TextView tv3;
    @BindView(R.id.recyview1)
    RecyclerView recyview1;
    @BindView(R.id.recyview2)
    RecyclerView recyview2;
    @BindView(R.id.scrollView)
    ScrollView scrollView;

    private TextView currTv;
    private String tabName = SgStrKey.dgk;
    private List<String> stringList = new ArrayList<>();
    private SuperAdapter<DPictureEntity> superAdapter1;
    private SuperAdapter<DPictureEntity> superAdapter2;
    private List<DPictureEntity> photos1 = new ArrayList<>();
    private List<DPictureEntity> photos2 = new ArrayList<>();
    private Activity activity;

    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    superAdapter1.notifyDataSetChanged();
                    break;
                case 2:
                    superAdapter2.notifyDataSetChanged();
                    break;
                case 3:
                    scrollView.fullScroll(ScrollView.FOCUS_UP);
                    break;
            }
            return false;
        }
    });

    @Override
    public int getLayoutId() {
        return R.layout.sg_activity_hideproinfo_browse;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        tabName = getIntent().getStringExtra("itemName");
        tv1.setText(tabName);
        changeTab();
        activity = this;
        recyview1.addItemDecoration(new DividerItemDecoration(context, LinearLayoutManager.HORIZONTAL));
        recyview1.setLayoutManager(RecyclerViewUtils.getLinearLayoutManagerForHorizontal(context));
        recyview1.setAdapter(getsuperAdapter1());
        recyview2.addItemDecoration(new DividerItemDecoration(context, LinearLayoutManager.HORIZONTAL));
        recyview2.setLayoutManager(RecyclerViewUtils.getLinearLayoutManagerForHorizontal(context));
        recyview2.setAdapter(getsuperAdapter2());
        handler.sendEmptyMessage(3);
        onrefresh();
    }

    private void onrefresh() {
        photos1.clear();
        photos2.clear();
        new Thread(new Runnable() {
            @Override
            public void run() {
                String value1 = String.valueOf(SPUtils.get(context, SgStrKey.uuidxm, ""));
                String value2 = String.valueOf(SPUtils.get(context, SgStrKey.uuidxx, ""));
                photos1.addAll(SgDBUtils.getDPictureList(value1, value2, "检查,xc"));
                handler.sendEmptyMessage(1);
                photos2.addAll(SgDBUtils.getDPictureList(value1, value2, "检查,tz"));
                handler.sendEmptyMessage(2);
            }
        }).start();
    }

    private RecyclerView.Adapter getsuperAdapter2() {
        superAdapter2 = new SuperAdapter<DPictureEntity>(R.layout.sg_content_photo_label1) {
            @Override
            public void bindView(ViewHolder viewHolder, final DPictureEntity photos, final int i) {
                ImageView imageView = viewHolder.getView(R.id.imageView);
                ImageButton imageButton = viewHolder.getView(R.id.imageButton);
                imageButton.setVisibility(View.GONE);
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                XUtils.loadImg(context, SgDBUtils.getUrl(photos), imageView);
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        List<ImageUrl> urlList = new ArrayList<>();
                        for (DPictureEntity data : photos2) {
                            String url = String.valueOf(SgDBUtils.getUrl(data));
                            urlList.add(new ImageUrl(url, null));
                        }
                        GPreviewBuilder.from(activity)
                                .setData(urlList)
                                .setCurrentIndex(i)
                                .setSingleFling(false)//是否在黑屏区域点击返回
                                .setDrag(false)//是否禁用图片拖拽返回
                                .setType(GPreviewBuilder.IndicatorType.Number)//指示器类型
                                .start();//启动
                    }
                });
            }
        };
        superAdapter2.setData(photos2);
        return superAdapter2;
    }

    private RecyclerView.Adapter getsuperAdapter1() {
        superAdapter1 = new SuperAdapter<DPictureEntity>(R.layout.sg_content_photo_label1) {
            @Override
            public void bindView(ViewHolder viewHolder, final DPictureEntity photos, final int i) {
                ImageView imageView = viewHolder.getView(R.id.imageView);
                ImageButton imageButton = viewHolder.getView(R.id.imageButton);
                imageButton.setVisibility(View.GONE);
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                XUtils.loadImg(context, SgDBUtils.getUrl(photos), imageView);
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        List<ImageUrl> urlList = new ArrayList<>();
                        for (DPictureEntity data : photos1) {
                            String url = String.valueOf(SgDBUtils.getUrl(data));
                            urlList.add(new ImageUrl(url, null));
                        }
                        GPreviewBuilder.from(activity)
                                .setData(urlList)
                                .setCurrentIndex(i)
                                .setSingleFling(false)//是否在黑屏区域点击返回
                                .setDrag(false)//是否禁用图片拖拽返回
                                .setType(GPreviewBuilder.IndicatorType.Number)//指示器类型
                                .start();//启动
                    }
                });
            }
        };
        superAdapter1.setData(photos1);
        return superAdapter1;
    }

    private void choiceType(final String title) {
        new ListDialog<String>()
                .setTitle(title)
                .setDatas(stringList)
                .setListener(new ResponseListener() {
                    @Override
                    public void response(Object... data) {
                        currTv.setText(data[0] + "");
                        if (title.contains("照片类别")) {
                        } else if (title.contains("图纸类别")) {
                        }
                    }
                }).show(getFragmentManager(), title);
    }

    private void changeTab() {
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        switch (tabName) {
            case SgStrKey.dgk:
                BrowsePolePitInfo ybgcInfo11 = new BrowsePolePitInfo();
                transaction.replace(R.id.flayout1, ybgcInfo11, tabName).commit();
                break;
            case SgStrKey.lxk:
                BrowseAnchorPitInfo ybgcInfo22 = new BrowseAnchorPitInfo();
                transaction.replace(R.id.flayout1, ybgcInfo22, tabName).commit();
                break;
            case SgStrKey.jdt:
                BrowseGroundInfo ybgcInfo33 = new BrowseGroundInfo();
                transaction.replace(R.id.flayout1, ybgcInfo33, tabName).commit();
                break;
            case SgStrKey.sbjc:
                BrowseBasicsWorkInfo ybgcInfo44 = new BrowseBasicsWorkInfo();
                transaction.replace(R.id.flayout1, ybgcInfo44, tabName).commit();
                break;
        }

    }

    @OnClick({R.id.tv2, R.id.tv3})
    public void onViewClicked(View view) {
        currTv = (TextView) view;
        switch (view.getId()) {
            case R.id.tv2:
//                stringList.clear();
//                stringList.add("全部照片");
//                stringList.add("挖坑情况");
//                stringList.add("土质情况");
//                choiceType("选择照片类别");
                break;
            case R.id.tv3:
//                stringList.clear();
//                stringList.add("全部照片");
//                stringList.add("设计图纸");
//                stringList.add("现场图纸");
//                choiceType("选择图纸类别");
                break;
        }
    }
}
